home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 501-525 / disk_521 / a68k / a68k271.lzh / A68kmisc.c < prev    next >
C/C++ Source or Header  |  1991-04-16  |  31KB  |  1,127 lines

  1. /*------------------------------------------------------------------*/
  2. /*                                    */
  3. /*            MC68000 Cross Assembler                */
  4. /*                                    */
  5. /*          Copyright 1985 by Brian R. Anderson            */
  6. /*                                    */
  7. /*              Miscellaneous routines - April 16, 1991            */
  8. /*                                    */
  9. /*   This program may be copied for personal, non-commercial use    */
  10. /*   only, provided that the above copyright notice is included        */
  11. /*   on all copies of the source code.  Copying for any other use   */
  12. /*   without the consent of the author is prohibited.            */
  13. /*                                    */
  14. /*------------------------------------------------------------------*/
  15. /*                                    */
  16. /*        Originally published (in Modula-2) in            */
  17. /*        Dr. Dobb's Journal, April, May, and June 1986.          */
  18. /*                                    */
  19. /*     AmigaDOS conversion copyright 1991 by Charlie Gibbs.        */
  20. /*                                    */
  21. /*------------------------------------------------------------------*/
  22.  
  23. #include "A68kdef.h"
  24. #include "A68kglb.h"
  25.  
  26. char Sdata[MAXSREC];    /* S-record data */
  27. int  Sindex;        /* Index for Sdata */
  28. int  NumRExt, NumR32, NumR16, NumR8;
  29.  
  30. static char *errmsg[] = {
  31.     "--- Unknown error code ---",
  32.     "Alignment error.",
  33.     "No such op-code.",
  34.     "Duplicate Symbol.",
  35.     "Undefined Symbol.",
  36.     "Addressing mode not allowed here.",
  37.     "Error in operand format.",
  38.     "Error in relative branch.",
  39.     "Address mode error.",
  40.     "Operand size error.",
  41.     "END statement is missing.",
  42.     "Value must be absolute.",
  43.     "Relocatability error.",
  44.     "INCLUDE file cannot be opened.",
  45.     "Illegal forward reference.",
  46.     "Not supported in S-format.",
  47.     "This instruction needs a label.",
  48.     "Pass 1 / Pass 2 phase error.",
  49.     "ENDM statement is missing.",
  50.     "ENDC statement is missing.",
  51.     "Unmatched ENDC statement.",
  52.     "Too much DC data.",
  53.     "Too many SECTIONs.",
  54.     "Duplicate macro definition.",
  55.     "More than one label on this line.",
  56.     "End of string is missing.",
  57.     "Short displacement can't be zero.",
  58.     ""};
  59.  
  60.  
  61.  
  62. long AddrBndW (v) register long v;
  63. /* Advances "v" to the next word boundary. */
  64. {
  65.     if (v & 1L) {
  66.     AppendSdata (0L, 1);
  67.     v++;
  68.     }
  69.     return (v);
  70. }
  71.  
  72.  
  73.  
  74. long AddrBndL (v) register long v;
  75. /* Advances "v" to the next long-word boundary. */
  76. {
  77.     long templong;
  78.  
  79.     v = AddrBndW (v);        /* Bump to a word boundary first. */
  80.     if (v & 2L) {        /* If still not aligned, */
  81.     templong = NOP;        /*  generate a NOP. */
  82.     AppendSdata (templong, 2);
  83.     v += 2;
  84.     }
  85.     return (v);
  86. }
  87.  
  88.  
  89.  
  90. void WriteListLine (f) struct fs *f;
  91. /* Writes one line to the Listing file, including Object Code. */
  92. {
  93.     register int i, j, printed;
  94.     long templong;
  95.     char macflag;
  96.     char tempstr[12];
  97.     int  dummy = 0;
  98.  
  99.     if (!Pass2)
  100.     return;            /* Pass 2 only */
  101.     if (FwdShort && (ErrLim == 0)) {
  102.     DisplayLine (dummy);
  103.     printf ("A short branch can be used here.\n");
  104.     }
  105.     if (SuppList)
  106.     return;            /* Listing is suppresed. */
  107.     if (ErrLim == 0)
  108.     if ((Dir == Page) || (Dir == Space) || (Dir == Title)
  109.     || (Dir == DoList) || (Dir == NoList) || (ListOff))
  110.         return;        /* Don't print unless they have errors. */
  111.  
  112.     CheckPage (f, FALSE);    /* Print headings if necessary. */
  113.  
  114.     if (PrntAddr) {
  115.     if ((Dir == Equ) || (Dir == Set))
  116.         LongPut (f, ObjSrc, 3);    /* Equated value */
  117.     else
  118.         LongPut (f, AddrCnt, 3);    /* Current location */
  119.     if (!KeepTabs)
  120.         xputs (f, "  ");
  121.     } else
  122.     if (!KeepTabs)
  123.         xputs (f, "        ");    /* Don't print location. */
  124.     if (KeepTabs)
  125.     xputs (f, "\t");        /* Use tabs for spacing. */
  126.     printed = 8;            /* We've printed 8 positions. */
  127.     LongPut (f, ObjOp, nO);        /* Generated code */
  128.     printed += nO * 2;
  129.     if (nS != 0) {
  130.     xputs (f, " ");
  131.     LongPut (f, ObjSrc, nS);
  132.     printed+= nS * 2 + 1;
  133.     }
  134.     if (nD != 0) {
  135.     xputs (f, " ");
  136.     LongPut (f, ObjDest, nD);
  137.     printed += nD * 2 + 1;
  138.     }
  139.     if ((j = nX) > 0) {                /* String data */
  140.     if ((j * 2 + printed) > ObjMAX)
  141.         j = (ObjMAX - printed) / 2;
  142.     for (i = 0; i < j; i++) {
  143.         templong = ObjString[i];
  144.         LongPut (f, templong, 1);
  145.     }
  146.     printed += j * 2;
  147.     }
  148.     while (printed < ObjMAX) {
  149.     if (KeepTabs) {
  150.         xputs (f, "\t");
  151.         printed += 8;
  152.         printed &= ~7;
  153.     } else {
  154.         xputs (f, " ");
  155.         printed++;
  156.     }
  157.     }
  158.     if ((InFNum == 0) || (OuterMac == 0))
  159.     macflag = ' ';            /* Open code */
  160.     else if (InFNum > OuterMac)
  161.     macflag = '+';            /* Inner macro */
  162.     else if ((InFNum == OuterMac) && (Dir != MacCall))
  163.     macflag = '+';            /* Outermost macro */
  164.     else
  165.     macflag = ' ';            /* We're outside macros. */
  166.  
  167.     sprintf (tempstr, "  %5d%c", LineCount, macflag);
  168.     xputs (f, tempstr);
  169.     xputs (f, Line);
  170.     xputs (f, "\n");
  171.  
  172.     if (FwdShort && (ErrLim == 0))
  173.     xputs (f, "A short branch can be used here.\n");
  174.  
  175.     for (i = 0; i < ErrLim; i++) {    /* Write error messages. */
  176.     CheckPage (f, FALSE);
  177.     xputs (f, errmsg[ErrCode[i]]);
  178.     printed = strlen(errmsg[ErrCode[i]]);
  179.     while (printed < ObjMAX + 8) {
  180.         if (KeepTabs) {
  181.         xputs (f, "\t");
  182.         printed += 8;
  183.         printed &= ~7;
  184.         } else {
  185.         xputs (f, " ");
  186.         printed++;
  187.         }
  188.     }
  189.     for (j = 0; j < ErrPos[i]; j++) {
  190.         if (Line[j] == '\t') {
  191.         xputs (f, "\t");
  192.         printed += 8;
  193.         printed &= ~7;
  194.         } else {
  195.         xputs (f, " ");
  196.         printed++;
  197.         }
  198.     }
  199.     xputs (f, "^ ");            /* Error flag */
  200.     if (i == 0) {
  201.         if (InF->UPtr == 0)
  202.         xputs (f, InF->NPtr);        /* Module name */
  203.         else
  204.         xputs (f, "(user macro)");    /* We're in a user macro. */
  205.         sprintf (tempstr, " line %d", InF->Line);
  206.         xputs (f, tempstr);            /* Line number */
  207.     }
  208.     xputs (f, "\n");
  209.     }
  210. }
  211.  
  212.  
  213.  
  214. void WriteSymTab (f) struct fs *f;
  215. /* Lists the symbol table in alphabetical order. */
  216. {
  217.     int  printhunk, i;
  218.     char *p;
  219.     char tempstr[MAXLINE];
  220.     long templong;
  221.     register int j, k;
  222.     register struct SymTab **ss1, **ss2, *sym, **sortlim;
  223.     struct Ref *ref;
  224.  
  225.     if (NumSyms == 0)
  226.     return;            /* The symbol table is empty - exit. */
  227.  
  228. /* Build a sorted table of pointers to symbol table entries. */
  229.  
  230.     templong = NumSyms * sizeof (struct SymTab *);
  231.     SymSort = (struct SymTab **) malloc ((unsigned) templong);
  232.     if (SymSort == NULL) {
  233.     fprintf (stderr, "Not enough memory for symbol table sort!\n");
  234.     return;
  235.     }
  236.     sortlim = SymSort + NumSyms;
  237.     sym = SymChunk = SymStart;
  238.     sym++;
  239.     SymChLim = (struct SymTab *) ((char *) SymChunk + CHUNKSIZE);
  240.     ss1 = SymSort;
  241.     while (sym) {
  242.     *ss1++ = sym;
  243.     sym = NextSym (sym);    /* Try for another symbol table entry. */
  244.     }
  245.     for (i = NumSyms / 2; i > 0; i /= 2) {        /* Shell sort */
  246.     for (ss1 = SymSort + i; ss1 < sortlim; ss1++) {    /*  (copied   */
  247.         for (ss2=ss1-i; ss2 >= SymSort; ss2 -= i) {    /*  from K&R) */
  248.         if (strcmp ((*ss2)->Nam, (*(ss2+i))->Nam) <= 0)
  249.             break;
  250.         sym = *ss2;
  251.         *ss2 = *(ss2+i);
  252.         *(ss2+i) = sym;
  253.         }
  254.     }
  255.     }
  256.  
  257. /* The table is now sorted - print the listing. */
  258.  
  259.     LnCnt = LnMax;            /* Skip to a new page. */
  260.     for (i = 0, ss1 = SymSort; i < NumSyms; i++) {
  261.     sym = *ss1++;
  262.     CheckPage (f, TRUE);
  263.  
  264.     p = sym->Nam;            /* Pointer to symbol */
  265.     if (sym->Flags & 8)
  266.         p++;            /* Skip blank preceding macro name. */
  267.     else if (sym->Flags & 0x10)
  268.         p += 6;            /* Skip hunk sequence number. */
  269.     sprintf (tempstr, "%-11s ", p);    /* Symbol or macro name */
  270.     xputs (f, tempstr);
  271.     if (strlen (p) > 11)        /* Long symbol - go to new line. */
  272.         if (KeepTabs)
  273.         xputs (f, "\n\t    ");
  274.         else
  275.         xputs (f, "\n            ");
  276.  
  277.     printhunk = FALSE;        /* Assume no hunk no. to print. */
  278.     if (sym->Defn == NODEF)
  279.         xputs (f, "  *** UNDEFINED *** ");
  280.     else if (sym->Flags & 4)
  281.         xputs (f, "  -- SET Symbol --  ");
  282.     else if (sym->Flags & 8) {
  283.         sprintf (tempstr, " +++ MACRO +++ %5d", sym->Defn);
  284.         xputs (f, tempstr);
  285.     } else if (sym->Flags & 0x10) {
  286.         j = (sym->Hunk & 0x3FFF0000L) >> 16;
  287.         if (j == HunkCode)
  288.         xputs (f, "  CODE    ");
  289.         else if (j == HunkData)
  290.         xputs (f, "  DATA    ");
  291.         else
  292.         xputs (f, "  BSS     ");
  293.         printhunk = TRUE;
  294.     } else if (sym->Flags & 0x20) {
  295.         sprintf (tempstr, "      %c%ld  ",
  296.         (sym->Val & 8L) ? 'A' : 'D', sym->Val & 7L);
  297.         xputs (f, tempstr);
  298.         printhunk = TRUE;
  299.     } else {
  300.         LongPut (f, sym->Val, 4);        /* Value */
  301.         xputs (f, "  ");
  302.         printhunk = TRUE;
  303.     }
  304.     if (printhunk) {
  305.         j = sym->Hunk & 0x00007FFFL;    /* Hunk number */
  306.         if (sym->Flags & 0x60)
  307.         xputs (f, " Reg");        /* Register or list */
  308.         else if (sym->Flags & 1)
  309.         xputs (f, " Ext");        /* External */
  310.         else if (j == ABSHUNK)
  311.         xputs (f, " Abs");        /* Absolute */
  312.         else {
  313.         sprintf (tempstr, "%4d", j);    /* Hunk number */
  314.         xputs (f, tempstr);
  315.         }
  316.         sprintf (tempstr," %5d",sym->Defn);    /* Statement number */
  317.         xputs (f, tempstr);
  318.     }
  319.     if (XrefList) {
  320.         xputs (f, "  ");
  321.         if (sym->Ref1 == NULL)
  322.         xputs (f, " *** UNREFERENCED ***");
  323.         else {
  324.         ref = sym->Ref1;
  325.         j = k = 0;
  326.         while (1) {
  327.             if (ref->RefNum[j] == 0)
  328.             break;
  329.             if (k >= 9) {
  330.             xputs (f, "\n");        /* New line */
  331.             if (KeepTabs)
  332.                 xputs (f, "\t\t\t\t  ");    /* 34 spaces */
  333.             else
  334.                 for (k = 0; k < 34; k++)
  335.                 xputs (f, " ");
  336.             k = 0;
  337.             }
  338.             sprintf (tempstr, "%5d", ref->RefNum[j]);
  339.             xputs (f, tempstr);
  340.             j++;
  341.             k++;
  342.             if (j < MAXREF)
  343.             continue;        /* Get the next slot. */
  344.             if ((ref = ref->NextRef) == 0)
  345.             break;            /* End of last entry */
  346.             j = 0;            /* Start the next entry. */
  347.         }
  348.         }
  349.     }
  350.     xputs (f, "\n");
  351.     }
  352.     free (SymSort);        /* Free the sort work area. */
  353.     SymSort = NULL;
  354. }
  355.  
  356.  
  357.  
  358. void CheckPage (f, xhdr) struct fs *f; int xhdr;
  359. /* Checks if end of page reached yet -- if so, advances to next page. */
  360. {
  361.     register int printed;
  362.     char tempstr[12];
  363.  
  364.     LnCnt++;
  365.     if (LnCnt >= LnMax) {
  366.     PgCnt++;
  367.     if (PgCnt > 1)
  368.         xputs (f, "\f");        /* Skip to new page. */
  369.     xputs (f, TTLstring);        /* Title */
  370.     printed = strlen (TTLstring);
  371.     while (printed < 56)
  372.         if (KeepTabs) {
  373.         xputs (f, "\t");
  374.         printed += 8;
  375.         printed &= ~7;
  376.         } else {
  377.         xputs (f, " ");
  378.         printed++;
  379.         }
  380.     xputs (f, SourceFN);        /* File name */
  381.     if (KeepTabs)
  382.         xputs (f, "\t");
  383.     else
  384.         xputs (f, "        ");
  385.     sprintf(tempstr, "Page %d\n\n", PgCnt);    /* Page number */
  386.     xputs (f, tempstr);
  387.     LnCnt = 2;
  388.     if (xhdr) {
  389.         xputs (f, "Symbol       Value    Hunk  Line");
  390.         if (XrefList)
  391.         xputs (f, "   References");    /* Cross-reference */
  392.         xputs (f, "\n\n");
  393.         LnCnt += 2;
  394.     }
  395.     }
  396. }
  397.  
  398.  
  399.  
  400. void StartSrec (f, idntname) struct fs *f; char *idntname;
  401. /* Writes object header record. */
  402. {
  403.     register long CheckSum, templong;
  404.     register char *s;
  405.  
  406.     if (SFormat) {
  407.     xputs (f, "S0");
  408.     templong = strlen (idntname) + 3;   /* extra for addr. & checksum */
  409.     LongPut (f, templong, 1);
  410.     CheckSum = templong;
  411.  
  412.     xputs (f, "0000");    /* Address is 4 digits, all zero, for S0. */
  413.  
  414.     s = idntname;
  415.     while (*s) {
  416.         templong = toupper (*s);
  417.         LongPut (f, templong, 1);
  418.         CheckSum += templong;
  419.         s++;
  420.     }
  421.     CheckSum = ~CheckSum;        /* Complement the checksum. */
  422.     LongPut (f, CheckSum, 1);
  423.     xputs (f, "\n");
  424.     } else {
  425.     templong = HunkUnit;
  426.     xputl (f, templong);
  427.     DumpName (f, idntname, 0L);
  428.     }
  429.     StartAddr = TempAddr = Sindex = 0;
  430.     NumRExt = NumR32 = NumR16 = NumR8 = 0;
  431. }
  432.  
  433.  
  434.  
  435. void WriteSrecLine (f) struct fs *f;
  436. /* Transfers object code components to output buffer. */
  437. /* Moves long words or portions thereof. */
  438. {
  439.     register int i, j;
  440.     register long templong;
  441.  
  442.     if (HunkType == HunkBSS)
  443.     return;                /* No code in BSS hunk! */
  444.  
  445.     if (nO + nS + nD + nX) {        /* If we have object code... */
  446.     AppendSdata (ObjOp, nO);    /* Opcode */
  447.     AppendSdata (ObjSrc, nS);    /* Source */
  448.     AppendSdata (ObjDest, nD);    /* Destination */
  449.     for (i = 0; i < DupFact; i++) {
  450.         for (j = 0; j < nX; j++) {    /* String data */
  451.         templong = ObjString[j];
  452.         AppendSdata (templong, 1);
  453.         }
  454.     }
  455.     }
  456. }
  457.  
  458.  
  459.  
  460. void AppendSdata (Data, n) register long Data; int n;
  461. /* If we are producing S-format records:
  462.      Transfers "n" low-order bytes from "Data" to the output buffer.
  463.      If the buffer becomes full, DumpSdata will be called to flush it.
  464.      S-records will also be broken on 16-byte boundaries.
  465.    If we are producing AmigaDOS format, data will be written
  466.      directly to Srec - we'll go back and fill in the hunk length
  467.      at the end of the hunk.  DumpSdata will never be called from here. */
  468. {
  469.     register int  i;
  470.     register char byte;
  471.     int dummy = 0;
  472.  
  473.     if (!Pass2)
  474.     return;            /* Pass 2 only */
  475.     if (HunkType == HunkBSS)
  476.     return;            /* No data in BSS hunks! */
  477.  
  478.     if (HunkType == HunkNone) {        /* We're not in a hunk yet - */
  479.     DoSection ("", 0, "", 0, "", 0);    /* start a code hunk. */
  480.     MakeHunk = TRUE;
  481.     }
  482.  
  483.     if (OrgFlag) {        /* If we've had an ORG directive */
  484.     FixOrg (dummy);        /*  do necessary adjustments     */
  485.     OrgFlag = FALSE;    /*  to the object code file.     */
  486.     }
  487.  
  488.     Data <<= (4 - n) * 8;    /* Left-justify data. */
  489.  
  490.     for (i = 0; i < n; i++) {
  491.     byte = (char) (Data >> ((3 - i) * 8));
  492.     TempAddr++;
  493.     if (!SFormat) {
  494.         xputc (byte, &Srec);
  495.     } else {
  496.         Sdata[Sindex++] = byte;
  497.         if (((TempAddr & 0x0F) == 0) || (Sindex >= MAXSREC))
  498.         DumpSdata (&Srec);    /* Break S-record. */
  499.     }
  500.     }
  501. }
  502.  
  503.  
  504.  
  505. void FixOrg (dummy) int dummy;
  506. /* Makes necessary adjustments to the object code file if an
  507.     ORG directive has been processed.  This routine is called
  508.     exclusively by AppendSdata and must only be called once for
  509.     each ORG encountered, when writing the next object code (if any). */
  510. {
  511.     register long templong;
  512.     register int  i;
  513.  
  514.     if (SFormat && (AddrCnt != TempAddr)) {    /* ORG in S-format -    */
  515.     DumpSdata (&Srec);            /*  dump current record */
  516.     StartAddr = TempAddr = AddrCnt;        /*  and start afresh.   */
  517.     }
  518.     if (AddrCnt < TempAddr) {        /* AmigaDOS backward ORG */
  519.     if (TempAddr > OrgHigh) {
  520.         LenPtr = NULL;
  521.         OrgHigh = TempAddr;    /* Save high address for return. */
  522.         xwrite (&Srec);            /* Flush the buffer. */
  523.         OrgSeek = lseek (Srec.fd, 0L, 1);    /* Remember position. */
  524.         lseek (Srec.fd,(AddrCnt & ~3L)-TempAddr,1);    /* New position */
  525.         if (AddrCnt    & 3L) {        /* If ORG isn't to long-word   */
  526.         read (Srec.fd, Srec.Buf, AddrCnt & 3L);    /*  move ahead */
  527.         lseek (Srec.fd, -(AddrCnt & 3L), 1);    /*  to keep    */
  528.         }                        /*  the buffer */
  529.         Srec.Ptr = Srec.Buf + (AddrCnt & 3L);    /*  aligned.   */
  530.     }
  531.     StartAddr = TempAddr = AddrCnt;
  532.  
  533.     } else if (AddrCnt > TempAddr) {    /* AmigaDOS forward ORG */
  534.     if (OrgHigh > TempAddr) {    /* Previous backward ORG */
  535.         if (AddrCnt < OrgHigh)
  536.         templong = AddrCnt;    /* Within previous range */
  537.         else
  538.         templong = OrgHigh;    /* Beyond previous range */
  539.         i = (int) (templong & 3L);    /* Alignment factor */
  540.         templong -= TempAddr;    /* Number of bytes to skip */
  541.         LenPtr = NULL;
  542.         xwrite (&Srec);            /* Flush the buffer. */
  543.         lseek (Srec.fd,templong-(long)i,1);    /* Skip written data. */
  544.         if (i) {        /* If skip isn't to long-word, */
  545.         read(Srec.fd,Srec.Buf,(long)i);    /*  move ahead */
  546.         lseek(Srec.fd, -((long) i), 1);    /*  to keep    */
  547.         }                    /*  the buffer */
  548.         Srec.Ptr = Srec.Buf + (long) i;    /*  aligned.   */
  549.         TempAddr += templong;
  550.         StartAddr = TempAddr;
  551.     }
  552.     while (TempAddr < AddrCnt) {    /* Extend with binary zeros. */
  553.         xputc (0, &Srec);
  554.         TempAddr++;
  555.     }
  556.     }
  557. }
  558.  
  559.  
  560.  
  561. void DumpSdata (f) register struct fs *f;
  562. /* Writes an object code record. */
  563. {
  564.     register long CheckSum, templong;
  565.     register char *s;
  566.     register int  i;
  567.  
  568.     if (!SFormat) {
  569.     if (AddrCnt < OrgHigh) {    /* If we did a backwards ORG, */
  570.         LenPtr = NULL;        /*  we have to fix things up. */
  571.         xwrite (&Srec);        /* Flush the buffer. */
  572.         lseek(f->fd,OrgSeek&~3L,0);    /* Back to high position */
  573.         if (OrgSeek & 3L) {        /* If ORG isn't to long-word,  */
  574.         read (f->fd, f->Buf, OrgSeek & 3L);    /*  move ahead */
  575.         lseek (f->fd, -(OrgSeek & 3L), 1);    /*  to keep    */
  576.         }                        /*  the buffer */
  577.         f->Ptr = f->Buf + (OrgSeek & 3L);        /*  aligned.   */
  578.         AddrCnt = StartAddr = TempAddr = OrgHigh;
  579.     }
  580.     AddrCnt = AddrBndL (AddrCnt);    /* Finish the last long word. */
  581.     templong = (AddrCnt - SectStart) >> 2;
  582.     if ((s = LenPtr) == NULL) {
  583.         xwrite (&Srec);            /* Flush the buffer. */
  584.         CheckSum = lseek (f->fd, 0L, 1);    /* Remember position. */
  585.         lseek (f->fd, LenPos, 0);        /* Put hunk length here. */
  586.         s = f->Buf;
  587.     }
  588.     *s++ = (char) (templong >> 24);
  589.     *s++ = (char) (templong >> 16);
  590.     *s++ = (char) (templong >> 8);
  591.     *s++ = (char) templong;
  592.     if (LenPtr == NULL) {
  593.         f->Ptr = f->Buf + 4;
  594.         xwrite (f);
  595.         lseek (f->fd, CheckSum, 0);    /* Go back to where we were. */
  596.         f->Ptr = f->Buf;
  597.     }
  598.     DumpRel (f);        /* Write relocation information. */
  599.     templong = HunkEnd;
  600.     xputl (f, templong);    /* End of the hunk */
  601.     TempAddr = AddrCnt;
  602.     return;
  603.     }
  604.  
  605.     if (Sindex == 0)
  606.      return;        /* There's nothing to dump. */
  607.  
  608.     xputs (f, "S2");
  609.     templong = Sindex + 4;    /* Record length */
  610.     LongPut (f, templong, 1);
  611.     CheckSum = templong;    /* Initialize CheckSum. */
  612.  
  613.     LongPut (f, StartAddr, 3);    /* Address */
  614.     CheckSum += (StartAddr >> 16) & 0x00FFL;
  615.     CheckSum += (StartAddr >> 8) & 0x00FFL;
  616.     CheckSum += StartAddr & 0x00FFL;
  617.  
  618.     for (i = 0; i < Sindex; i++) {
  619.     templong = Sdata[i];
  620.     LongPut (f, templong, 1);    /* Object code */
  621.     CheckSum += templong;
  622.     }
  623.     CheckSum = ~CheckSum;    /* Complement the checksum. */
  624.     LongPut (f, CheckSum, 1);
  625.     xputs (f, "\n");
  626.  
  627.     StartAddr += Sindex;
  628.     TempAddr = StartAddr;
  629.     Sindex = 0;
  630. }
  631.  
  632.  
  633.  
  634. void PutRel (addr, hunk, size) long addr, hunk; int size;
  635. /* Build a relocation entry if necessary. */
  636. {
  637.     register struct RelTab *rel;
  638.  
  639.     if (!Pass2)
  640.     return;                /* Pass 2 only */
  641.     if (SFormat)
  642.     return;                /* Not for S-format! */
  643.     if (hunk == ABSHUNK)
  644.     return;                /* Absolute */
  645.     if (HunkType == HunkBSS)
  646.     return;                /* Not for BSS hunks! */
  647.  
  648.     rel = RelLim;            /* Pointer to new entry. */
  649.     RelLim++;                /* Bump limit pointer. */
  650.     if (((char *) RelLim - (char *) RelCurr) > CHUNKSIZE) {
  651.     rel = (struct RelTab *) malloc ((unsigned) CHUNKSIZE);
  652.     if (rel == NULL)
  653.         quit_cleanup ("Out of memory!\n");
  654.     RelCurr->Link = rel;        /* Link from previous chunk */
  655.     RelCurr = rel;            /* Make the new chunk current. */
  656.     RelCurr->Link = NULL;        /* Clear forward pointer. */
  657.     rel++;                /* Skip over pointer entry. */
  658.     RelLim = rel;            /* New table limit */
  659.     RelLim++;            /* Bump it. */
  660.     }
  661.     if (RelLast != NULL)
  662.     RelLast->Link = rel;        /* Link from previous entry */
  663.     rel->Link = NULL;            /* End of the chain (so far) */
  664.     rel->Offset = addr;            /* Offset */
  665.     rel->Hunk = hunk;            /* Hunk number */
  666.     rel->Size = size;            /* Size */
  667.     RelLast = rel;            /* Pointer to last entry in chain */
  668.  
  669.     if (hunk < 0)            /* Count entries by type. */
  670.     NumRExt++;
  671.     else if (size == Long)
  672.     NumR32++;
  673.     else if (size == Word)
  674.     NumR16++;
  675.     else
  676.     NumR8++;
  677. }
  678.  
  679.  
  680.  
  681. void DumpRel (f) struct fs *f;
  682. /* Dump relocation information to the object file. */
  683. {
  684.     register struct SymTab *sym;
  685.     register struct RelTab *rel, *rel2;
  686.     int  j, size, num, donexhdr, secthlin;
  687.     long currhunk, nexthunk, templong;
  688.     char *s, *t;
  689.  
  690.     secthlin = LineCount;        /* Current section ends here */
  691.     if ((Dir == Section)        /*   unless we're starting   */
  692.     || (Dir == CSeg)            /*      a new section.       */
  693.     || (Dir == DSeg)
  694.     || (Dir == BSS))
  695.     secthlin--;            /* Then it ends at previous line. */
  696.  
  697.     if (SFormat)
  698.     return;                /* S-format is absolute! */
  699.  
  700.     while (1) {
  701.     if ((num = NumR32) != 0) {
  702.         size = Long;        /* Do 32-bit fields */
  703.         templong = HunkR32;
  704.         NumR32 = 0;            /* ...but only once. */
  705.     } else if ((num = NumR16) != 0) {
  706.         size = Word;        /* Then do 16-bit fields. */
  707.         templong = HunkR16;
  708.         NumR16 = 0;
  709.     } else if ((num = NumR8) != 0) {
  710.         size = Byte;        /* Finally do 8-bit fields. */
  711.         templong = HunkR8;
  712.         NumR8 = 0;
  713.     } else
  714.         break;            /* We're all done. */
  715.  
  716.     xputl (f, templong);        /* Record type */
  717.  
  718.     currhunk = 32767;
  719.     num = 0;
  720.     if (rel = RelStart)    /* If we have anything, */
  721.         rel++;        /*  skip over the first chunk's link. */
  722.     while (rel) {
  723.         if ((rel->Size == size) && (rel->Hunk >= 0)) {
  724.         if (rel->Hunk < currhunk) {
  725.             currhunk = rel->Hunk;    /* Lowest hunk number */
  726.             num = 1;            /* Reset counter. */
  727.         } else if (rel->Hunk == currhunk) {
  728.             num++;            /* Count entries. */
  729.         }
  730.         }
  731.         rel = rel->Link;
  732.     }
  733.     while (num > 0) {    /* Repeat for all hunk references. */
  734.         templong = num;
  735.         xputl (f, templong);    /* Number of entries */
  736.         xputl (f, currhunk);    /* Hunk number */
  737.         nexthunk = 32767;
  738.         num = 0;            /* Count for next hunk */
  739.         if (rel = RelStart)
  740.         rel++;
  741.         while (rel) {
  742.         if ((rel->Size == size) && (rel->Hunk >= 0)) {
  743.             if (rel->Hunk < currhunk) {
  744.             rel = rel->Link;    /* We already wrote it. */
  745.             continue;
  746.             } else if (rel->Hunk == currhunk) {
  747.             xputl (f, rel->Offset - SectStart);
  748.             } else if (rel->Hunk < nexthunk) {
  749.             nexthunk = rel->Hunk;    /* Next hunk number */
  750.             num = 1;        /* Reset counter. */
  751.             } else if (rel->Hunk == nexthunk) {
  752.             num++;            /* Count entries. */
  753.             }
  754.         }
  755.         rel = rel->Link;
  756.         }
  757.         currhunk = nexthunk;    /* Get ready for next hunk. */
  758.     }
  759.     xputl (f, 0L);        /* End of relocation information */
  760.     }
  761.  
  762.     donexhdr = FALSE;        /* We haven't written hunk_ext yet. */
  763.  
  764.     sym = SymChunk = SymStart;
  765.     sym++;
  766.     SymChLim = (struct SymTab *) ((char *) SymChunk + CHUNKSIZE);
  767.     while (sym) {
  768.     if (sym->Flags & 2) {        /* Scan for XDEF symbols. */
  769.         j = sym->Defn;    /* Defined in current section? */
  770.         if ((j >= SectLine) && (j <= secthlin)) {
  771.         if (!donexhdr) {
  772.             templong = HunkExt;    /* We haven't done header yet. */
  773.             xputl (f, templong);
  774.             donexhdr = TRUE;
  775.         }
  776.         if ((sym->Hunk & 0x0000FFFFL) == ABSHUNK)
  777.             templong = 0x02000000;
  778.         else
  779.             templong = 0x01000000;        /* Flags */
  780.         DumpName (f, sym->Nam, templong);    /* Symbol */
  781.         xputl (f, sym->Val - SectStart);    /* Offset */
  782.         }
  783.     }
  784.     sym = NextSym (sym);
  785.     }
  786.  
  787.     if (NumRExt != 0) {            /* External references (XREF) */
  788.     if (!donexhdr) {
  789.         templong = HunkExt;        /* We haven't done header yet. */
  790.         xputl (f, templong);
  791.         donexhdr = TRUE;
  792.     }
  793.     if (rel = RelStart)
  794.         rel++;
  795.     while (rel) {
  796.         if (rel->Hunk < 0) {
  797.         size = rel->Size;
  798.         if (size == Long)
  799.             templong = 0x81000000L;    /* ext_ref32 */
  800.         else if (size == Word)
  801.             templong = 0x83000000L;    /* ext_ref16 */
  802.         else
  803.             templong = 0x84000000L;    /* ext_ref8 */
  804.         s = (char *) ~(rel->Hunk);
  805.         DumpName (f, s, templong);    /* Flags and symbol */
  806.         templong = 1;
  807.         rel2 = rel->Link;
  808.         while (rel2) {
  809.             if ((rel2->Hunk == rel->Hunk) && (rel2->Size == size))
  810.             templong++;        /* Number of times */
  811.             rel2 = rel2->Link;        /*  symbol occurs  */
  812.         }
  813.         xputl (f, templong);
  814.         rel2 = rel;            /* Now go back and  */
  815.         while (rel2) {            /*  write them out. */
  816.             if ((rel2->Hunk==rel->Hunk) && (rel2->Size==size)) {
  817.             xputl(f, rel2->Offset - SectStart); /* Offset */
  818.             if (rel2 != rel)    /* Kill hunk so we    */
  819.                 rel2->Hunk = 0;    /*  don't do it again */
  820.             }                /*  (we're done with  */
  821.             rel2 = rel2->Link;        /*  the table anyway). */
  822.         }
  823.         }
  824.         rel = rel->Link;
  825.     }
  826.     NumRExt = 0;
  827.     }
  828.     if (donexhdr)
  829.     xputl (f, 0L);            /* End of external information */
  830.  
  831.     if (DumpSym) {            /* Dump the symbol table. */
  832.     donexhdr = FALSE;
  833.     sym = SymChunk = SymStart;
  834.     sym++;
  835.     SymChLim = (struct SymTab *) ((char *) SymChunk + CHUNKSIZE);
  836.     for (; sym; sym = NextSym (sym)) {
  837.         s = DumpSymList;
  838.         if (*s) {        /* Select by prefix. */
  839.         if (*s == '!')
  840.             s++;    /* Symbols must NOT match prefix. */
  841.         t = sym->Nam;
  842.         j = TRUE;    /* Assume we have a match. */
  843.         while (*s) {
  844.             if (*s++ != *t++) {
  845.             j = FALSE;    /* Symbol does not match prefix. */
  846.             break;
  847.             }
  848.         }
  849.         if (DumpSymList[0] == '!') {
  850.             if (j)
  851.             continue;
  852.         } else {
  853.             if (!j)
  854.             continue;
  855.         }
  856.         }
  857.         if ((sym->Hunk & 0x0000FFFFL) == CurrHunk) {
  858.         j = sym->Flags & 0x7F;        /* Ignore PUBLIC flag. */
  859.         if ((j == 0) || (j == 2)) {    /* Defined, may be XDEF. */
  860.             if ((sym->Defn >= SectLine) && (sym->Defn <= secthlin)) {
  861.             if (!donexhdr) {    /* It's in current SECTION. */
  862.                 templong = HunkSym;
  863.                 xputl (f, templong);    /* Write header */
  864.                 donexhdr = TRUE;        /* if necessary. */
  865.             }
  866.             DumpName (f, sym->Nam, 0L);    /* Symbol */
  867.             xputl(f, sym->Val - SectStart);    /* Offset */
  868.             }
  869.         }
  870.         }
  871.     }
  872.     if (donexhdr)
  873.         xputl (f, 0L);        /* End of symbol table dump */
  874.     }
  875.  
  876.     rel = RelStart->Link;
  877.     while (rel != NULL) {
  878.     rel2 = rel;
  879.     rel = rel2->Link;
  880.     free (rel2);            /* Free all but the first chunk. */
  881.     }
  882.     RelCurr = RelStart;            /* The first chunk is current. */
  883.     RelCurr->Link = NULL;        /* Unlink additional chunks. */
  884.     RelLast = NULL;            /* There are no entries left. */
  885.     RelLim = RelStart;
  886.     RelLim++;                /* First unused space */
  887. }
  888.  
  889.  
  890.  
  891. void EndSdata (f, addr) struct fs *f; long addr;
  892. /* Write end record to object file. */
  893. {
  894.     register long checksum;
  895.  
  896.     if (SFormat) {
  897.     DumpSdata (f);            /* Write any remaining data. */
  898.     xputs (f, "S804");        /* Record header */
  899.     checksum = 4;
  900.     LongPut (f, addr, 3);        /* Transfer address */
  901.     checksum += (addr >> 16) & 0x00FFL;
  902.     checksum += (addr >> 8) & 0x00FFL;
  903.     checksum += addr & 0x00FFL;
  904.     checksum = ~checksum;
  905.     LongPut (f, checksum, 1);    /* Checksum */
  906.     xputs (f, "\n");
  907.     } else {
  908.     if (HunkType != HunkNone) {
  909.         DumpSdata (f);        /* Last hunk's data */
  910.     }
  911.     }
  912. }
  913.  
  914.  
  915.  
  916. void DumpName (f, name, flags) struct fs *f; char *name; long flags;
  917. /* Writes a name preceded by a long word containing the
  918.     length of the name in long words.  The length word has
  919.     the contents of "flags" ORed into it.  The name is padded
  920.     with binary zeros to the next long word boundary. */
  921. {
  922.     register int  i;
  923.     register long templong;
  924.  
  925.     i = strlen (name);
  926.     templong = (i + 3) >> 2;    /* Length of name (long words) */
  927.     templong |= flags;        /* Add flag bits. */
  928.     xputl (f, templong);    /* Write length and flags. */
  929.     xputs (f, name);        /* Write the name itself. */
  930.     while (i & 3) {
  931.     xputc ('\0', f);    /* Pad the last word. */
  932.     i++;
  933.     }
  934. }
  935.  
  936.  
  937.  
  938. void LongPut (f, data, length) struct fs *f; long data; int length;
  939. /* Writes to file "f" the hexadecimal interpretation of
  940.     the bytes in "data".  The number of bytes written
  941.     (two hex digits per byte) is given in "length" -
  942.     if less than 4, only low-order bytes are written. */
  943. {
  944.     register int i, j;
  945.     register char *t;
  946.     char xstr[9];
  947.  
  948.     t = xstr;
  949.     for (i = length * 8 - 4; i >= 0; i -= 4) {
  950.     j = (int) ((data >> i) & 0x0FL);
  951.     *t++ = (char) ((j > 9) ? (j - 10 + 'A') : (j + '0'));
  952.     }
  953.     *t = '\0';
  954.     xputs (f, xstr);
  955. }
  956.  
  957.  
  958.  
  959. int xopen (name, f, desc) char *name; struct fs *f; char *desc;
  960. /* Opens the output file whose name is in "name",
  961.     setting up the file structure pointed to by "f".
  962.     This routine first allocates a file buffer -
  963.     if unsuccessful, it calls quit_cleanup.
  964.     Otherwise, it opens the file - if unsuccessful,
  965.     displays an error message using "desc" and returns TRUE.
  966.     If the file is successfully opened, this routine returns FALSE. */
  967. {
  968.     if ((f->Buf = (char *) malloc (BUFFSIZE)) == NULL)
  969.     quit_cleanup ("Out of memory!\n");
  970.     if ((f->fd = creat (name, 0644)) < 0) {
  971.     fprintf (stderr, "Unable to open %s file.\n", desc);
  972.     f->fd = NULL;
  973.     return (TRUE);
  974.     }
  975.     f->Ptr = f->Buf;
  976.     f->Lim = f->Buf + BUFFSIZE;
  977.     return (FALSE);
  978. }
  979.  
  980.  
  981.  
  982. void xputs (f, s) struct fs *f; register char *s;
  983. /* Writes the string pointed to by "s"
  984.     to the output file whose structure is pointed to by "f". */
  985. {
  986.     register char *t, *l;
  987.  
  988.     t = f->Ptr;        /* Current position (use registers for speed) */
  989.     l = f->Lim;        /* End of buffer */
  990.  
  991.     while (*s) {
  992.     *t++ = *s++;
  993.     if (t >= l) {
  994.         f->Ptr = t;
  995.         xwrite (f);        /* Flush the buffer. */
  996.         if (f == &Srec)
  997.         LenPtr = NULL;    /* Hunk length is no longer in buffer. */
  998.         t = f->Buf;        /* Reset pointer. */
  999.     }
  1000.     }
  1001.     f->Ptr = t;            /* Update pointer. */
  1002. }
  1003.  
  1004.  
  1005.  
  1006. void xputl (f, data) register struct fs *f; register long data;
  1007. /* Writes to file "f" the contents of the long word in "data". */
  1008. {
  1009.     xputc ((char) (data >> 24), f);
  1010.     xputc ((char) (data >> 16), f);
  1011.     xputc ((char) (data >> 8), f);
  1012.     xputc ((char) data, f);
  1013. }
  1014.  
  1015.  
  1016.  
  1017. void xputc (byte, f) char byte; register struct fs *f;
  1018. /* Writes the byte contained in "byte" to file "f". */
  1019. {
  1020.     register char *t;
  1021.  
  1022.     t = f->Ptr;        /* Current position (use a register for speed) */
  1023.  
  1024.     *t++ = byte;
  1025.     if (t >= f->Lim) {
  1026.     f->Ptr = t;
  1027.     xwrite (f);        /* Flush the buffer. */
  1028.     if (f == &Srec)
  1029.         LenPtr = NULL;    /* Hunk length is no longer in buffer. */
  1030.     t = f->Buf;        /* Reset pointer. */
  1031.     }
  1032.     f->Ptr = t;            /* Update pointer. */
  1033. }
  1034.  
  1035.  
  1036.  
  1037. void xclose (f) struct fs *f;
  1038. /* Closes the output file whose structure is pointed to by "f".
  1039.     The buffer is flushed if necessary, then freed.        */
  1040. {
  1041.     xwrite (f);        /* Flush the buffer. */
  1042.     close (f->fd);    /* Close the file. */
  1043.     f->fd = NULL;
  1044.     free (f->Buf);    /* Free the buffer. */
  1045.     f->Buf = NULL;
  1046. }
  1047.  
  1048.  
  1049.  
  1050. void xwrite (f) struct fs *f;
  1051. /* General write routine */
  1052. {
  1053.     int i;
  1054.  
  1055.     i = (int) (f->Ptr - f->Buf);    /* Number of bytes to write */
  1056.     if (i > 0) {
  1057.     if (write (f->fd, f->Buf, i) != i) {    /* Write the buffer. */
  1058.         f->Ptr = f->Buf;        /* Avoid loop in xclose()! */
  1059.         if (f == &List)
  1060.         quit_cleanup ("\nListing file write error!\n");
  1061.         else if (f == &Eq)
  1062.         quit_cleanup ("\nEquate file write error!\n");
  1063.         else
  1064.         quit_cleanup ("\nObject file write error!\n");
  1065.     }
  1066.     }
  1067. }
  1068.  
  1069.  
  1070. void Error (pos, errornum) int pos, errornum;
  1071. /* Displays error message #errornum.  If this is the first error for
  1072.     the current line, the line itself is displayed, preceded by a
  1073.     message giving the current position in the current module.
  1074.     If the line is in a macro or include file, the position in
  1075.     each nested module is given, working out to the source file.
  1076.     A flag is placed under the column indicated by "pos".       */
  1077. {
  1078.     register int i;
  1079.     int dummy = 0;
  1080.  
  1081.     if (!Pass2 && (errornum != NoIncl)) {
  1082.     if (IncStart != 0) {            /* Don't skip this     */
  1083.         IncStart = 0;            /*  INCLUDE file in    */
  1084.         if (SkipLim->Set1 != NULL) {    /*  pass 2 - we must   */
  1085.         SetFixLim = SkipLim->Set1;    /*  re-read it to      */
  1086.         SetFixLim++;            /*  report its errors. */
  1087.         }
  1088.     }
  1089.     return;                /* Report during pass 2 only. */
  1090.     }
  1091.     if (ErrLim < ERRMAX) {        /* Save error data. */
  1092.     ErrCode[ErrLim] = errornum;
  1093.     ErrPos[ErrLim] = pos;
  1094.     ErrLim++;
  1095.     }
  1096.     if (ErrLim == 1)        /* If this is the first error for this line, */
  1097.     DisplayLine (dummy);    /*  display the line and its number(s). */
  1098.     printf ("\t");
  1099.     for (i = 0; i < pos; i++)
  1100.     if (Line[i] == '\t')
  1101.         printf ("\t");
  1102.     else
  1103.         printf(" ");        /* Space over to error column. */
  1104.     printf ("^ %s\n",errmsg[errornum]);    /* Error flag and message */
  1105.     ErrorCount++;            /* Count errors. */
  1106. }
  1107.  
  1108.  
  1109.  
  1110. void DisplayLine (dummy) int dummy;
  1111. /* Displays the current line and its position
  1112.     in all current files - used by Error, etc. */
  1113. {
  1114.     register struct InFCtl *inf;
  1115.     register int i;
  1116.  
  1117.     printf ("\n");
  1118.     for (i = InFNum, inf = InF; i >= 0; i--, inf++) {    /* Nested? */
  1119.     if (inf->UPtr == 0)
  1120.         printf ("%s", inf->NPtr);        /* Module name */
  1121.     else
  1122.         printf ("(user macro)");        /* We're in a user macro. */
  1123.     printf (" line %d\n", inf->Line);    /* Line number in module */
  1124.     }
  1125.     printf ("%5d   %s\n", LineCount, Line);    /* The line itself */
  1126. }
  1127.